# Bar System 服务状态检查脚本

$ErrorActionPreference = "Continue"

Write-Host "=== Bar System 服务状态 ===" -ForegroundColor Cyan

# 检查后端服务
Write-Host "`n后端服务 (端口 38521):" -ForegroundColor Yellow
$backendConnection = Test-NetConnection -ComputerName localhost -Port 38521 -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
if ($backendConnection.TcpTestSucceeded) {
    Write-Host "  状态: 运行中 ✓" -ForegroundColor Green
    $backendProcess = Get-NetTCPConnection -LocalPort 38521 -ErrorAction SilentlyContinue | 
        Select-Object -First 1 -ExpandProperty OwningProcess
    if ($backendProcess) {
        $proc = Get-Process -Id $backendProcess -ErrorAction SilentlyContinue
        Write-Host "  进程ID: $backendProcess" -ForegroundColor Gray
        Write-Host "  内存使用: $([Math]::Round($proc.WorkingSet64/1MB, 2)) MB" -ForegroundColor Gray
    }
} else {
    Write-Host "  状态: 未运行 ✗" -ForegroundColor Red
}

# 检查Web前端服务
Write-Host "`nWeb前端服务 (端口 35280):" -ForegroundColor Yellow
$webConnection = Test-NetConnection -ComputerName localhost -Port 35280 -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
if ($webConnection.TcpTestSucceeded) {
    Write-Host "  状态: 运行中 ✓" -ForegroundColor Green
    $webProcess = Get-NetTCPConnection -LocalPort 35280 -ErrorAction SilentlyContinue | 
        Select-Object -First 1 -ExpandProperty OwningProcess
    if ($webProcess) {
        $proc = Get-Process -Id $webProcess -ErrorAction SilentlyContinue
        Write-Host "  进程ID: $webProcess" -ForegroundColor Gray
        Write-Host "  内存使用: $([Math]::Round($proc.WorkingSet64/1MB, 2)) MB" -ForegroundColor Gray
    }
} else {
    Write-Host "  状态: 未运行 ✗" -ForegroundColor Red
}

# 显示访问地址
Write-Host "`n=== 服务访问地址 ===" -ForegroundColor Cyan
Write-Host "后端API: http://localhost:38521" -ForegroundColor White
Write-Host "Web管理: http://localhost:35280" -ForegroundColor White

# 显示日志位置
Write-Host "`n=== 日志文件 ===" -ForegroundColor Cyan
Write-Host "后端: D:\test\bar\backend\logs\service.log" -ForegroundColor Gray
Write-Host "Web: D:\test\bar\web\logs\service.log" -ForegroundColor Gray
